package cn.abcsys.devops.application.restful;/**
 * Copyright (2017, ) Institute of Software, Chinese Academy of Sciences
 * Copyright (2017, ) Bocloud Co,. Lmt
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author xuyuanjia2017@otcaix.iscsa.ac.cn
 * @date 2017/6/20 0020
 * say sth.
 */
@Component("deployerRequest")
public class DeployerRequest {

    @Autowired
    private RestTemplate template;

    private String baseUrl;

    public DeployerRequest(String ip){
        this.baseUrl=ip+":9003/devops-deployer/";
    }

    public DeployerRequest(){
        this.baseUrl="http://127.0.0.1:9003/devops-deployer/";
    }

    public void showDefault(){
        //template.getFor
        System.out.println(template.getForObject(this.baseUrl+"test.do", Map.class));
    }

    public Map getCreateRc(Map<String,Object> params){
        try {
            return template.getForObject(this.baseUrl+"createAndStart.do?" +
                    "instanceCore.instanceNamespace={instanceCore.instanceNamespace}&" +
                    "instanceCore.instanceName={instanceCore.instanceName}&" +
                    "instanceCore.ip={instanceCore.ip}&" +
                    "instanceCore.instanceType={instanceCore.instanceType}&" +
                    "instanceCore.instanceImage={instanceCore.instanceImage}&" +
                    "instanceCore.instanceCpu={instanceCore.instanceCpu}&" +
                    "instanceCore.instanceMemory={instanceCore.instanceMemory}&" +
                    "instanceCore.instanceNetwork={instanceCore.instanceNetwork}&" +
                    "instanceCore.instanceImagePullSecret={instanceCore.instanceImagePullSecret}&" +
                    "instanceCore.appName={instanceCore.appName}&" +
                    "instanceCore.cmd={instanceCore.cmd}&" +
                    "instanceEnvsListString={instanceEnvsListString}&" +
                    "instanceVolumesListString={instanceVolumesListString}&" +
                    "instanceCore.applicationRuntimeId={instanceCore.applicationRuntimeId}&" +
                    "instanceCore.appId={instanceCore.appId}&" +
                    "instancePortsString={instancePortsString}", Map.class,params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Map getDeleteRc(Map<String,Object> params){
        try{
            return template.getForObject(this.baseUrl+"delete.do?" +
            "instanceCore.instanceCoreId={instanceCore.instanceCoreId}",
                    Map.class,params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Map getContainersFromAppName(Map<String,Object> params){
        try{
            return template.getForObject(this.baseUrl+"getContainersFromAppName.do?" +
                            "imageNameTag={imageNameTag}&" +
                            "page={page}&" +
                            "rows={rows}&" +
                            "appName={appName}",
                    Map.class,params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public RestTemplate getTemplate() {
        return template;
    }

    public void setTemplate(RestTemplate template) {
        this.template = template;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public static void main(String[] args) {
        DeployerRequest dr = new DeployerRequest("127.0.0.1");
        dr.showDefault();
    }

}
